package com.xf.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xf.auth.entity.UserConnection;
import com.xf.auth.mapper.UserConnectionMapper;
import com.xf.auth.mapper.UserMapper;
import com.xf.auth.service.UserConnectionService;
import com.xf.auth.service.UserService;
import com.xf.common.core.entity.system.SystemUser;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * Todo
 *
 * @author xufeng
 * @version 1.0
 * @date 2021/12/1
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, SystemUser> implements UserService {
    @Resource
    private PasswordEncoder passwordEncoder;


    /**
     * 注册用户 0.1版本先写简单点 后面扩展
     *
     * @param username 用户名
     * @param password 密码
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean register(String username, String password) {
        SystemUser oldUser = this.getOne(new LambdaQueryWrapper<SystemUser>().eq(SystemUser::getUsername, username));
        if (!Objects.isNull(oldUser)) {
            return true;
        }
        SystemUser systemUser = new SystemUser();
        systemUser.setUsername(username);
        systemUser.setPassword(password);
        systemUser.setPassword(passwordEncoder.encode(SystemUser.DEFAULT_PASSWORD));
        systemUser.setStatus("1");
        return this.save(systemUser);
    }
}
